package com.exampleyyy.demoyyy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exampleyyy.demoyyy.JieKouSHITIYYY.CategoryJieKou;
import com.exampleyyy.demoyyy.JieKouService.CategoryServiceJieKou;
import com.exampleyyy.demoyyy.SHITIYYY.Category;

import com.exampleyyy.demoyyy.SHITIYYY.Dish;
import com.exampleyyy.demoyyy.SHITIYYY.Setmeal;
import com.exampleyyy.demoyyy.common.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryService  extends ServiceImpl<CategoryJieKou, Category> implements CategoryServiceJieKou {
    @Autowired
    private DishService dishService;
    // 装配水果服务层
    @Autowired
    private SetmealService setmealService;
    // 装配套餐服务层
    @Override // 重写
    public void shanchu(Long ids) {
        //添加查询条件，根据分类id进行查询水果数据
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper();
       // 创建查询对象 分类表的ids 在Dish表中有没有
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        // ids = 177050
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //如果已经关联，抛出一个业务异常
        if(count1>0){
            throw new CustomException("当前分类下关联了水果，不能删除");
            //  throw 抛出异常
            //已经关联水果，抛出一个业务异常
        }
        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
            //已经关联套餐，抛出一个业务异常
        }
        //正常删除分类
        super.removeById(ids);
        // 子类传递给父类
    }
}